﻿<%@ WebHandler Language="C#" Class="denglu" %>

using System;
using System.Web;
using System.Data;
using KeaoBase;
using System.Configuration;
using System.Text;
using ServiceStack.Redis;

public class denglu : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";

        try
        {
            string changePwd = context.Request.Form["changepwd"];
            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
            if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
            {
                context.Response.Write(string.Format("{2}\"uid\":\"{0}\",\"status\":\"failed\",\"err\":\"{1}\"{3}", "0", "数据库连接失败", "{", "}"));
                return;
            }
            else
            {
                if (!string.IsNullOrEmpty(changePwd))
                {
                    string uid = context.Request.Form["pwd-uid"];
                    string kid = context.Request.Form["pwd-kid"];
                    if (!Auth.UserLogined(uid, kid))
                    {
                        context.Response.Write("Failed");
                        return;
                    }
                    string oldPwd = context.Request.Form["oldpwd"];
                    string newPwd = context.Request.Form["newpwd"];
                    string createCondition = m_DB.CreateAndCondition("id", DataValueCommonType.字符串, changePwd, "", SearchConditionType.等于);
                    createCondition += m_DB.CreateAndCondition("yonghumima", DataValueCommonType.字符串, KeaoUtils.Encrypt(oldPwd), "", SearchConditionType.等于);
                    DataTable yongHuDt = m_DB.GetDataTableByCondition("tbl_yonghu", createCondition);
                    if (yongHuDt.Rows.Count > 0)
                    {
                        TableRow editRow = new TableRow("tbl_yonghu", changePwd, DBCtl.修改);
                        TableCol col1 = new TableCol();
                        col1.ColName = "yonghumima";
                        col1.ValueMember = KeaoUtils.Encrypt(newPwd);
                        col1.ValueType = DataValueCommonType.字符串;
                        editRow.DataCols.Add(col1);
                        if (m_DB.SaveTableRows(editRow) > 0)
                        {
                            context.Response.Write("OK");
                            return;
                        }
                        else
                        {
                            context.Response.Write("Failed");
                            return;
                        }
                    }
                    else
                    {
                        context.Response.Write("没有找到用户信息或用户原始密码错误，请检查重试...");
                        return;
                    }
                }
                else
                {
                    string loginName = context.Request.Form["name"];
                    string loginPwd = context.Request.Form["pwd"];

                    string createCondition = m_DB.CreateAndCondition("yonghudengluming", DataValueCommonType.字符串, loginName, "", SearchConditionType.等于);
                    createCondition += m_DB.CreateAndCondition("yonghumima", DataValueCommonType.字符串, KeaoUtils.Encrypt(loginPwd), "", SearchConditionType.等于);
                    DataTable Dt = m_DB.GetDataTableByCondition("tbl_yonghu", createCondition);
                    if (Dt.Rows.Count > 0)
                    {
                        createCondition = m_DB.CreateAndCondition("juesemingcheng", DataValueCommonType.字符串, string.Format("{0}", Dt.Rows[0]["yonghujuese"]), "", SearchConditionType.等于);
                        DataTable roleDt = m_DB.GetDataTableByCondition("tbl_juese", createCondition);
                        string quanXianStr = string.Format("{0}", roleDt.Rows[0]["juesequanxian"]);
                        if (!quanXianStr.Contains("登录系统"))
                        {
                            context.Response.Write(string.Format("{2}\"uid\":\"{0}\",\"status\":\"failed\",\"err\":\"{1}\"{3}", "0", "用户没有登录权限", "{", "}"));
                            return;
                        }
                        StringBuilder tagBuilder = new StringBuilder();
                        tagBuilder.Append("\"tags\":[");
                        if (quanXianStr.Contains("用户管理"))
                        {
                            tagBuilder.Append("{\"id\":\"xinxi-yonghu\",\"text\":\"用户管理\",\"iconCls\":\"tree-icon tree-file\"},");
                            tagBuilder.Append("{\"id\":\"xinxi-juese\",\"text\":\"角色管理\",\"iconCls\":\"tree-icon tree-file\"},");
                        }
                        if (quanXianStr.Contains("单位管理"))
                        {
                            tagBuilder.Append("{\"id\":\"xinxi-gongyingshang\",\"text\":\"供应商管理\",\"iconCls\":\"tree-icon tree-file\"},");
                            tagBuilder.Append("{\"id\":\"xinxi-danwei\",\"text\":\"单位管理\",\"iconCls\":\"tree-icon tree-file\"},");
                        }
                        if (quanXianStr.Contains("物料管理"))
                        {
                            tagBuilder.Append("{\"id\":\"xinxi-wuliao\",\"text\":\"物料管理\",\"iconCls\":\"tree-icon tree-file\"},");
                        }
                        if (quanXianStr.Contains("标段管理"))
                        {
                            //tagBuilder.Append("{\"id\":\"xinxi-biaoduan\",\"text\":\"路基施工段落\",\"iconCls\":\"tree-icon tree-file\"},");
                            //tagBuilder.Append("{\"id\":\"xinxi-handong\",\"text\":\"涵洞用材料管理\",\"iconCls\":\"tree-icon tree-file\"},");
                            //tagBuilder.Append("{\"id\":\"xinxi-jizhuang\",\"text\":\"基桩用材料管理\",\"iconCls\":\"tree-icon tree-file\"},");
                            //tagBuilder.Append("{\"id\":\"xinxi-qiaoliang\",\"text\":\"桥梁用材料管理\",\"iconCls\":\"tree-icon tree-file\"},");
                            //tagBuilder.Append("{\"id\":\"xinxi-suidao\",\"text\":\"隧道用材料管理\",\"iconCls\":\"tree-icon tree-file\"},");
                            //tagBuilder.Append("{\"id\":\"xinxi-qita\",\"text\":\"其它材料去向\",\"iconCls\":\"tree-icon tree-file\"},");
                        }
                        if (quanXianStr.Contains("运输公司管理"))
                        {
                            tagBuilder.Append("{\"id\":\"xinxi-yunshugongsi\",\"text\":\"运输公司管理\",\"iconCls\":\"tree-icon tree-file\"},");
                        }
                        if (quanXianStr.Contains("基础数据配置"))
                        {
                            tagBuilder.Append("{\"id\":\"xinxi-zidian\",\"text\":\"数据字典管理\",\"iconCls\":\"tree-icon tree-file\"},");
                            tagBuilder.Append("{\"id\":\"xinxi-shezhiyuejie\",\"text\":\"设置月结日期\",\"iconCls\":\"tree-icon tree-file\"},");
                        }
                        tagBuilder.Append("{\"id\":\"xinxi-xiugaimima\",\"text\":\"修改密码\",\"iconCls\":\"tree-icon tree-file\"}");
                        tagBuilder.Append("]");
                        if (quanXianStr.Contains("偏差设置"))
                        {
                            tagBuilder.Append(",\"rukupiancha\":[");
                            tagBuilder.Append("{\"id\":\"bangfang-ruku-piancha\",\"text\":\"偏差汇总查询\",\"iconCls\":\"tree-icon tree-file\"}],\"chukupiancha\":[");
                            tagBuilder.Append("{\"id\":\"bangfang-chuku-piancha\",\"text\":\"偏差汇总查询\",\"iconCls\":\"tree-icon tree-file\"}]");
                        }
                        string uid = string.Format("{0}", Dt.Rows[0]["id"]);
                        string kid = Guid.NewGuid().ToString();
                        createCondition = m_DB.CreateAndCondition("fenzu", DataValueCommonType.字符串, "月结", "", SearchConditionType.等于);
                        DataTable yueJieDt = m_DB.GetDataTableByCondition("tbl_zidian", createCondition);
                        string yueJieStr = string.Format("{0}", yueJieDt.Rows[0]["xiangmu"]);
                        using (IRedisClient redis = RedisClientPool.GetRedisClient())
                        {
                            if (redis!=null&&!redis.HadExceptions)
                            {
                                redis.SetEntryInHash(uid, "kid", kid);
                                redis.SetEntryInHash(uid, "pwd", string.Format("{0}", Dt.Rows[0]["yonghumima"]));
                                redis.SetEntryInHash(uid, "yuejie", yueJieStr);
                            }
                        }
                        context.Response.Write(string.Format("{5}\"uid\":\"{0}\",\"kid\":\"{8}\",\"kehubianhao\":\"{7}\",\"status\":\"success\",\"err\":\"{1}\",\"yonghu\":\"{2}\",\"juese\":\"{3}\",{4}{6}", uid, "登录成功", Dt.Rows[0]["yonghuxingming"], Dt.Rows[0]["yonghujuese"], tagBuilder.ToString(), "{", "}",ConfigurationManager.AppSettings["客户编号"],kid));
                    }
                    else
                    {
                        context.Response.Write(string.Format("{2}\"uid\":\"{0}\",\"status\":\"failed\",\"err\":\"{1}\"{3}", "0", "用户名或密码错误", "{", "}"));
                    }
                }
            }

        }
        catch (Exception err)
        {
            KeaoUtils.LogTxt(string.Format("登录异常。{0}", err.Message));
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}